import 'dart:io';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:extended_image/extended_image.dart';
import 'package:example/window_page.dart';

pictureView({
  @required BuildContext context,
  @required List<PicsItemModel> pics,
  int index = 0,
}) {
  windowPage(
    context: context,
    builder: (BuildContext context) {
      return _PictureView(
        pics: pics,
        currentIndex: index,
      );
    },
  );
}

class _PictureView extends StatefulWidget {
  _PictureView({
    @required this.pics,
    @required this.currentIndex,
  });

  final List<PicsItemModel> pics;
  final int currentIndex;

  @override
  _PictureViewState createState() {
    return _PictureViewState();
  }
}

class _PictureViewState extends State<_PictureView> {
  int currentIndex;

  @override
  void initState() {
    super.initState();
    currentIndex = widget.currentIndex;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ExtendedImageSlidePage(
        slideAxis: SlideAxis.both,
        slideType: SlideType.onlyImage,
        resetPageDuration: const Duration(milliseconds: 200),
        slidePageBackgroundHandler: slidePageBackgroundHandler,
        child: ExtendedImageGesturePageView.builder(
          itemCount: widget.pics.length,
          onPageChanged: (int index) {
            currentIndex = index;
          },
          controller: PageController(
            initialPage: currentIndex,
          ),
          scrollDirection: Axis.horizontal,
          itemBuilder: (BuildContext context, int index) {
            Widget image;
            final item = widget.pics[index];

            if (item.local) {
              image = ExtendedImage.file(
                File(item.uri),
                fit: BoxFit.contain,
                mode: ExtendedImageMode.Gesture,
                enableSlideOutPage: true,
              );
            } else {
              image = ExtendedImage.network(
                item.uri,
                fit: BoxFit.contain,
                mode: ExtendedImageMode.Gesture,
                enableSlideOutPage: true,
              );
            }
            image = Container(
              child: image,
              padding: EdgeInsets.all(5.0),
            );
            if (index == currentIndex) {
              return Hero(
                tag: item.uri + index.toString(),
                child: image,
              );
            } else {
              return image;
            }
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}

Color slidePageBackgroundHandler(
  Offset offset,
  Size pageSize,
) {
  double opacity = 0.0;
  opacity = offset.distance /
      (Offset(pageSize.width, pageSize.height).distance / 2.0);

  return Colors.black.withOpacity(min(1.0, max(1.0 - opacity, 0.0)));
}

class PicsItemModel {
  PicsItemModel({
    this.uri,
    this.local = false,
  });

  final String uri;
  final bool local; // 是否本地路径
}
